<?php

namespace App\Observers;

use App\Models\Article\ArticleInfo;
use App\Models\Article\ArticleOrder;
use App\Models\User\MemberOrder;
use App\Models\User\User;
use EasyWeChat\Factory;

class ArticleOrderObserver
{

    public function saved(ArticleOrder $articleOrder){

        if($articleOrder->is_finished==1){
            $user = User::where('id',$articleOrder->user_id)->first();
            $article = ArticleInfo::where('id',$articleOrder->article_id)->first();
            $config = config('wechat.default');
            $app = Factory::officialAccount($config);
            $templateId = 'E9SO_96RwhzI9fQwgLjF2A-w1VX-2x115UsNA64kzXo';
            $openids = [
                'oU4Mm6DyveBqszbNBkbcHVFuAtmw',
                //'oU4Mm6Onl4pbgnvPjkwLHBDdajvY',
            ];
            $todayFee = MemberOrder::whereDate('created_at',date('Y-m-d'))->where('is_finished',1)->sum('fee')+
                ArticleOrder::whereDate('created_at',date('Y-m-d'))->where('is_finished',1)->sum('fee');
            $remark = "昵称:$user->nick_name($user->id)";
            foreach ($openids as $openid) {
                $app->template_message->send([
                    'touser' => $openid,
                    'template_id' => $templateId,
                    'url' => '',
                    'data' => [
                        'keyword1' => $articleOrder->fee.'元',
                        'keyword2' => $article->title??'未知',//收款内容
                        'keyword3' => $articleOrder->created_at,//收款时间
                        'keyword4' => '永久',//交易状态
                        'keyword5' => $remark.'  总收益:'.$todayFee //交易说明
                    ],
                    /*'data' => [
                        'productType' => $article->title??'未知',
                        'name' => '购买者昵称 - '.$user->nick_name??$user->open_id,
                        'number' => '1 X '.$articleOrder->fee.'元',
                        'expDate' => '永久',
                        'remark' => '备注：用户ID - '.$user->id.'  总收益：'.$todayFee
                    ],*/
                ]);
            }

        }
    }

    /**
     * Handle the ArticleOrder "created" event.
     *
     * @param  \App\Models\Article\ArticleOrder  $articleOrder
     * @return void
     */
    public function created(ArticleOrder $articleOrder)
    {
        //
    }

    /**
     * Handle the ArticleOrder "updated" event.
     *
     * @param  \App\Models\Article\ArticleOrder  $articleOrder
     * @return void
     */
    public function updated(ArticleOrder $articleOrder)
    {
        //
    }

    /**
     * Handle the ArticleOrder "deleted" event.
     *
     * @param  \App\Models\Article\ArticleOrder  $articleOrder
     * @return void
     */
    public function deleted(ArticleOrder $articleOrder)
    {
        //
    }

    /**
     * Handle the ArticleOrder "restored" event.
     *
     * @param  \App\Models\Article\ArticleOrder  $articleOrder
     * @return void
     */
    public function restored(ArticleOrder $articleOrder)
    {
        //
    }

    /**
     * Handle the ArticleOrder "force deleted" event.
     *
     * @param  \App\Models\Article\ArticleOrder  $articleOrder
     * @return void
     */
    public function forceDeleted(ArticleOrder $articleOrder)
    {
        //
    }
}
